/**
 * 
 */
package org.myontology.specialpages.configs;

import java.util.ArrayList;
import java.util.List;

import org.myontology.controller.MockAttributeHolder;
import org.myontology.specialpages.EntryRow;
import org.myontology.specialpages.helpers.LanguageCheck;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.RepositoryConnection;

import controller.sesameDataConnector.AbstractDataConnector;

/**
 * @author hacklm
 *
 */
public class NotEnglishLabelPage extends AbstractSerqlSpecialPageConfig {

	public NotEnglishLabelPage(String query,String name) {
		super("", name);
		setKey(getClass().getName());
		this.query=query;
	}
	
protected  String query;
	
	

	/* (non-Javadoc)
	 * @see org.myontology.specialpages.AbstractSpecialPageConfig#select()
	 */
	@Override
	protected List<EntryRow> select() {
		
		List<EntryRow> ret = new ArrayList<EntryRow>();
		LanguageCheck check = new LanguageCheck();
		try{
			RepositoryConnection con = AbstractDataConnector.getConnection();	
			try {
				logger.info("special page query: "+query);
				TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query);
				TupleQueryResult result = tupleQuery.evaluate();
				try {
					if( result.hasNext()) {
								
						while (result.hasNext()) {
							BindingSet set = result.next();
							
							EntryRow row = writeSetToRow(set);
							String label = row.getLabel();
							if (!check.checkLabel(label, new MockAttributeHolder()))
								ret.add(row);
						}
					}
				} finally {
					result.close();
				}
			} 
			catch (Exception e) {
				logger.error(e.getMessage());
			}
			finally {
				con.close();
			}
		} catch (Exception e) {
			logger.error("Error while selecting " + e.getMessage());
			return null;
		}
		return ret;
		
		
	}

}
